import 'package:netflix_social/models/user.dart';
import 'package:netflix_social/models/video.dart';

class SocialPost {
  final String id;
  final User author;
  final String content;
  final List<String> images;
  final Video? relatedVideo;
  final DateTime createdAt;
  final int likeCount;
  final int commentCount;
  final int shareCount;
  final bool isLiked;
  final List<Comment> comments;
  final String? activityType; // 例如：观影打卡，影评，分享等

  SocialPost({
    required this.id,
    required this.author,
    required this.content,
    this.images = const [],
    this.relatedVideo,
    required this.createdAt,
    this.likeCount = 0,
    this.commentCount = 0,
    this.shareCount = 0,
    this.isLiked = false,
    this.comments = const [],
    this.activityType,
  });

  factory SocialPost.fromJson(Map<String, dynamic> json) {
    return SocialPost(
      id: json['id'],
      author: User.fromJson(json['author']),
      content: json['content'],
      images: List<String>.from(json['images'] ?? []),
      relatedVideo: json['related_video'] != null
          ? Video.fromJson(json['related_video'])
          : null,
      createdAt: DateTime.parse(json['created_at']),
      likeCount: json['like_count'] ?? 0,
      commentCount: json['comment_count'] ?? 0,
      shareCount: json['share_count'] ?? 0,
      isLiked: json['is_liked'] ?? false,
      comments: json['comments'] != null
          ? List<Comment>.from(
              json['comments'].map((x) => Comment.fromJson(x)))
          : [],
      activityType: json['activity_type'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'author': author.toJson(),
      'content': content,
      'images': images,
      'related_video': relatedVideo?.toJson(),
      'created_at': createdAt.toIso8601String(),
      'like_count': likeCount,
      'comment_count': commentCount,
      'share_count': shareCount,
      'is_liked': isLiked,
      'comments': comments.map((x) => x.toJson()).toList(),
      'activity_type': activityType,
    };
  }

  SocialPost copyWith({
    String? id,
    User? author,
    String? content,
    List<String>? images,
    Video? relatedVideo,
    DateTime? createdAt,
    int? likeCount,
    int? commentCount,
    int? shareCount,
    bool? isLiked,
    List<Comment>? comments,
    String? activityType,
  }) {
    return SocialPost(
      id: id ?? this.id,
      author: author ?? this.author,
      content: content ?? this.content,
      images: images ?? this.images,
      relatedVideo: relatedVideo ?? this.relatedVideo,
      createdAt: createdAt ?? this.createdAt,
      likeCount: likeCount ?? this.likeCount,
      commentCount: commentCount ?? this.commentCount,
      shareCount: shareCount ?? this.shareCount,
      isLiked: isLiked ?? this.isLiked,
      comments: comments ?? this.comments,
      activityType: activityType ?? this.activityType,
    );
  }
}

class Comment {
  final String id;
  final User author;
  final String content;
  final DateTime createdAt;
  final int likeCount;
  final bool isLiked;

  Comment({
    required this.id,
    required this.author,
    required this.content,
    required this.createdAt,
    this.likeCount = 0,
    this.isLiked = false,
  });

  factory Comment.fromJson(Map<String, dynamic> json) {
    return Comment(
      id: json['id'],
      author: User.fromJson(json['author']),
      content: json['content'],
      createdAt: DateTime.parse(json['created_at']),
      likeCount: json['like_count'] ?? 0,
      isLiked: json['is_liked'] ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'author': author.toJson(),
      'content': content,
      'created_at': createdAt.toIso8601String(),
      'like_count': likeCount,
      'is_liked': isLiked,
    };
  }

  Comment copyWith({
    String? id,
    User? author,
    String? content,
    DateTime? createdAt,
    int? likeCount,
    bool? isLiked,
  }) {
    return Comment(
      id: id ?? this.id,
      author: author ?? this.author,
      content: content ?? this.content,
      createdAt: createdAt ?? this.createdAt,
      likeCount: likeCount ?? this.likeCount,
      isLiked: isLiked ?? this.isLiked,
    );
  }
}
